package com.example.demo.controller;

import com.example.demo.annotation.AuthCheck;
import com.example.demo.common.BaseResponse;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.ResultUtils;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ThrowUtils;
import com.example.demo.myModel.entity.Role;
import com.example.demo.myModel.otd.user.AddRoleRequest;
import com.example.demo.service.My.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {



@Resource
private RoleService roleService;

    /**
     * 增加角色
     *
     * @param addRoleRequest 请求实体
     * @param request        请求信息
     * @return {
     * code:200,
     * date:3
     * }
     */
    @PostMapping("/addRole")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Integer> addRole(@RequestBody AddRoleRequest addRoleRequest, HttpServletRequest request) {
        if (addRoleRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Role role = new Role();
        BeanUtils.copyProperties(addRoleRequest, role);
        boolean result = roleService.save(role);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(role.getRoleId());
    }
}
